#!/usr/bin/env python3

"""
Nagios plugin to check the length of the FTS update log.
"""
import sys

sys.path.append("/home/zulip/deployments/current")
try:
    from scripts.lib.setup_path import setup_path

    setup_path()
except ImportError:
    pass

import psycopg2

states = {
    "OK": 0,
    "WARNING": 1,
    "CRITICAL": 2,
    "UNKNOWN": 3,
}


def report(state: str, num: str) -> None:
    print(f"{state}: {num} rows in fts_update_log table")
    exit(states[state])


conn = psycopg2.connect(database="zulip")
cursor = conn.cursor()

cursor.execute("SELECT count(*) FROM fts_update_log")
num = cursor.fetchall()[0][0]

if num > 5:
    report("CRITICAL", num)

report("OK", num)
